package cn.yitun.chat.yitunserverimcore.general.main.biz.contact.manager;

import cn.yitun.chat.yitunservercommonlib.converter.dto2vo.contact.ContactCategoryDto2VoConverter;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.contact.ContactCategoryDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.contact.ContactCategoryVo;
import cn.yitun.chat.yitunservercommonlib.enums.userChat.UserCategoryDefaultEnums;
import cn.yitun.chat.yitunserverimbase.dao.contact.ContactCategoryDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 
 * Date 2019-01-20 17:50:07<br>
 * Description
 * 
 * @author yitun<br>
 * @version 1.1
 */
@Service
public class ContactCategoryManager {

	@Resource
	private ContactCategoryDao contactCategoryDAO;

	/**
	 * 
	 * Date 2019-01-20 17:48:44<br>
	 * Description 获取默认的联系人分组，没有则创建
	 * 
	 * @author yitun<br>
	 * @param userId
	 * @return
	 * @version 1.1
	 */
	public ContactCategoryVo getOrCreateDefault(Long userId) {
		ContactCategoryVo bean = contactCategoryDAO.getVOByUserId(userId);
		if (null == bean) {
			bean = new ContactCategoryVo();
			bean.setUserId(userId);
			bean.setName("我的好友");
			bean.setRank(0);
			bean.setSort(UserCategoryDefaultEnums.SORT_DEFAULT.getType());
			ContactCategoryDto dto = ContactCategoryDto2VoConverter.INSTANCE.vo2dto(bean);
			contactCategoryDAO.save(dto);

			if (bean.getId() == null)
				bean.setId(dto.getId());
		}

		return bean;
	}

	/**
	 * 
	 * Date 2019-01-20 17:51:28<br>
	 * Description 获取默认分组id
	 * 
	 * @author yitun<br>
	 * @param userId
	 * @return
	 * @version 1.1
	 */
	public Long getOrCreateDefaultCategoryId(Long userId) {
		ContactCategoryVo bean = getOrCreateDefault(userId);
		return null != bean ? bean.getId() : null;
	}
}
